package com.bawei.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.tencent.mmkv.MMKV

/**
 * Author: Jason Zhang
 * Date: 2024/8/21
 * Desc: 路由参数拦截器
 */

@Interceptor(priority = 5, name = "路由参数拦截器")
class RouteParamsInterceptor: IInterceptor {

    private lateinit var mContext: Context

    //需要拦截下来Path
    private val needInterceptorPaths = arrayOf("/activity/detail", "/activity/list")

    override fun init(context: Context?) {
        if (context != null){
            mContext = context
        }
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        var routePath = postcard.path
        //获取路由参数
        var routeParams = postcard.extras
        var sessionId = MMKV.defaultMMKV().decodeString("sessionId", "123232")

        if (needInterceptorPaths.contains(routePath) && !routeParams.containsKey("sessionId")){
            postcard.withString("sessionId", sessionId)
        }

        //继续执行跳转逻辑
        callback.onContinue(postcard)
    }


}